package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.PageResult;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddLink;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.service.YddLinkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {

    @Autowired
    private YddLinkService yddLinkService;


    /**
     * 连接详情
     */
    @GetMapping("/rowInfo")
    public R roInfo(String id) {
        YddLink link = yddLinkService.getById(id);
        return R.success(link);
    }

    /**
     * 链接修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YddLink yddLink) {
        yddLinkService.updateById(yddLink);
        return R.success("修改成功");
    }


    /**
     * 链接删除
     */
    @GetMapping("/del")
    public R delete(String id) {
        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, YddLink::getId, id);
        YddLink one = yddLinkService.getOne(queryWrapper);
        if (one.getDefaultData() != null) {
            return R.error("系统默认数据,不能删除");
        }
        yddLinkService.removeById(id);
        return R.success("删除成功");

    }

    /**
     * 链接添加
     */
    @PostMapping("/add")
    public R add(@RequestBody YddLink yddLink) {
        yddLinkService.save(yddLink);
        return R.success("添加成功");
    }

    /**
     * 链接列表
     */
    @GetMapping("/list")
    public R<PageDto> list(@RequestParam Map map) {
        //1、获取前端传来的数据
        String page = map.get("page").toString();
        String limit = map.get("limit").toString();
        String type = (String) map.get("type");

        //构造分页构造器
        Page<YddLink> pageInfo = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));

        //条件构造器
        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件 类型
        queryWrapper.eq(type != null, YddLink::getType, type);
        //执行查询
        yddLinkService.page(pageInfo, queryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getRecords());
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setTotalPages((long) Math.ceil((double) pageInfo.getTotal() / pageInfo.getSize()));
        pageResult.setPageSize(pageInfo.getSize());
        pageResult.setCurrentPage(pageInfo.getCurrent());

        PageDto<YddLink> pageDto = new PageDto<>();
        BeanUtils.copyProperties(pageInfo, pageDto);

        pageDto.setValue(pageInfo);
        return R.success(pageDto);


    }
}
